#include "AConnection.h"


AConnection::AConnection(ConnCallback successCb, ConnCallback errorCb, QObject *parent)
    :QObject(parent), mSuccessCb(successCb), mErrorCb(errorCb)
{
    connect(&mSocket, SIGNAL(connected()), this, SLOT(onConnected()));
    connect(&mSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
}

void AConnection::setHostAddress(QHostAddress &addr)
{
    mIpAddress  = addr;
}

void AConnection::setPort(int port)
{
    mPort  = port;
}

void AConnection::close()
{
    mSocket.close();
}

void AConnection::startConnect()
{
    mSocket.connectToHost(mIpAddress, mPort);
}

void AConnection::startConnect(QHostAddress &addr, int port)
{
    setHostAddress(addr);
    setPort(port);
    startConnect();
}

void AConnection::onConnected()
{
    qDebug() << "On connected";
    if(mSuccessCb != NULL){
        *mSuccessCb;
    }
}

void AConnection::onError(QAbstractSocket::SocketError)
{
    qDebug() << "On error";
    if(mErrorCb != NULL){
        *mErrorCb;
    }
}
